package com.haierCamera.customapplication.ui.main;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.BaseRequestOptions;
import com.bumptech.glide.request.RequestOptions;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklAdapterAlarmDeviceBinding;
import com.haierCamera.customapplication.ui.business.entity.AlarmMessageInfo;
import com.haierCamera.customapplication.ui.business.util.ImageHelper;
import com.haierCamera.customapplication.ui.custom.BaseListenerOnClik;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.utils.DateUtil;
import com.haierCamera.customapplication.utils.DensityUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

public class AlarmMessageListAdapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterAlarmDeviceBinding>> {

    private List<AlarmMessageInfo> data;
    private Context context;
    private FragmentActivity activity;
    private BaseListenerOnClik click;

    public AlarmMessageListAdapter(Context context) {
        this.context = context;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    public void upDate(List<AlarmMessageInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnItemOnClick(BaseListenerOnClik click) {
        this.click = click;
    }

    @Override
    public DataBoundViewHolder<HzklAdapterAlarmDeviceBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        Log.i("TAG","LeaveMessageAdapter_onCreateViewHolder");
        HzklAdapterAlarmDeviceBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context)
                , R.layout.hzkl_adapter_alarm_device, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterAlarmDeviceBinding> holder, int i) {
        Log.i("TAG","onBindViewHolder-" + data.get(i).getName());
        holder.binding.tvName.setText(data.get(i).getName());
        holder.binding.tvTime.setText(data.get(i).getLocalDate().substring(5
                ,data.get(i).getLocalDate().length()-3));
//        holder.binding.tvTime.setText(DateUtil.getDateToString(Long.valueOf(data.get(i).getTime()),"MM-dd HH:mm:ss") +"" );
        holder.binding.tvContent.setText(getTypeStr(data.get(i).getType()));

        holder.itemView.setOnClickListener((v)->{
            context.startActivity(new Intent(context,HZKLAlarmMessageIntradayActivity.class)
                    .putExtra("devId",data.get(i).getDeviceId())
                    .putExtra("cameraPwd",data.get(i).getDeviceKey())
                    .putExtra("name",data.get(i).getName())
                    .putExtra("chnId", data.get(i).getChnlUuid()));
        });

        if(data.get(i).getPicUrl() != null && data.get(i).getPicUrl().length() > 0) {

            ImageHelper.loadCacheImage(data.get(i).getPicUrl()
                    ,data.get(i).getDeviceId()
                    ,data.get(i).getDeviceKey() != null ? data.get(i).getDeviceKey() : data.get(i).getDeviceId()
                    , new Handler(){
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            try {
                                if(data.get(i).getPicUrl().hashCode() == msg.what && msg.obj != null){
//                                    holder.binding.ivScale.setImageDrawable((Drawable)msg.obj);
                                    //得到资源文件的BitMap
//                                    Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.dog);

                                    BitmapDrawable bd = (BitmapDrawable) ((Drawable)msg.obj);
                                    Bitmap image= bd.getBitmap();
                                    Log.i("TAG","hhh-" + image.getHeight());
                                    Log.i("TAG","h80-" + DensityUtil.dip2px(context,80));
                                    //创建RoundedBitmapDrawable对象
                                    RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(context.getResources(),image);
                                    //抗锯齿
                                    roundImg.setAntiAlias(true);
                                    //设置圆角半径
                                    roundImg.setCornerRadius(DensityUtil.dip2px(context,20));
                                    holder.binding.ivScale.setImageDrawable(roundImg);

                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }
    }

    private String getTypeStr(int t) {
        switch (t) {
            case 0:
                return "人体红外";

            case 1:
                return "动态检测";

            case 2:
                return "未知告警";

            case 3:
                return "低电压告警";

            case 4:
                return "配件人体红外检测";

            case 5:
                return "移动感应器发生移动事件";

            case 6:
                return "移动感应器长时间未发生移动事件";

            case 7:
                return "配件人体红外检测长久未报警事件";

            case 8:
                return "门磁报警事件";

            case 90:
                return "报警网关门磁探测报警";

            case 91:
                return "报警网关红外探测报警";

            case 92:
                return "报警网关低电量报警";

            case 93:
                return "报警网关防拆报警";

                default:
                    return "";

        }
    }

    @Override
    public int getItemCount() {
        if (data!=null) {
            return data.size();
        } else {
            return 0;
        }
    }
}
